package com.capri.app;

import android.app.Activity;
import android.app.Application;

import com.capri.activity.MainActivity;
import com.capri.service.GetPsuhService;
import com.capri.service.PushService;
import com.squareup.leakcanary.LeakCanary;

import java.util.ArrayList;
import java.util.List;

public class NurseHomeApp extends Application {

    private static NurseHomeApp instance;
    /**
     * 打开的activity
     **/
    private List<Activity> activities = new ArrayList<Activity>();

    public static NurseHomeApp getInstance(){
//        if(instance==null){
//            synchronized (NurseHomeApp.class){
//                if(instance==null){
//                    instance=new NurseHomeApp();
//                }
//            }
//        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        if (LeakCanary.isInAnalyzerProcess(this)) {
            //此过程专用于LeakCanary进行堆分析。在此过程中不应初始化应用程序。
            return;
        }
        LeakCanary.install(this);
        com.igexin.sdk.PushManager.getInstance().initialize(getApplicationContext(), PushService.class);
        com.igexin.sdk.PushManager.getInstance().registerPushIntentService(getApplicationContext(), GetPsuhService.class);
    }

    /**
     * 新建了一个activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    public void finishOtherActivities() {
        List<Activity> localActivities = new ArrayList<>();
        if (activities != null && activities.size() > 0) {
            for (Activity activity : activities) {
                if (activity != null) {
                    if (!activity.getClass().getName().equals(MainActivity.class.getName())) {
//                        this.activities.remove(activity);
                        activity.finish();
                    } else {
                        localActivities.add(activity);
                    }
                }
            }
            activities.clear();
            activities.addAll(localActivities);
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            this.activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    public void finishClzActivity(Class clz){
        List<Activity> localActivities = new ArrayList<>();
        if (activities != null && activities.size() > 0) {
            for (Activity activity : activities) {
                if (activity != null) {
                    if (!activity.getClass().getName().equals(clz.getName())) {
                        activity.finish();
                    } else {
                        localActivities.add(activity);
                    }
                }
            }
            activities.clear();
            activities.addAll(localActivities);
        }
    }
}
